package dp;

import java.util.ArrayList;
import java.util.List;

/**
 * leet-code(78) 子集
 *
 * @author Ringo
 * @date 2021/8/18 21:33
 */
public class Subsets {

    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        System.out.println(newInstance().subsets(nums));
    }

    public static Subsets newInstance() {
        return new Subsets();
    }

    public List<List<Integer>> subsets(int[] nums) {
        if (nums == null || nums.length == 0)
            return new ArrayList<>();
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        dfs(nums, 0, ans, list);
        return ans;
    }

    private void dfs(int[] nums, int index, List<List<Integer>> ans, List<Integer> list) {
        ans.add(new ArrayList<>(list));
        for (int i = index; i < nums.length; ++i) {
            list.add(nums[i]);
            // 注意这里
            dfs(nums, i + 1, ans, list);
            list.remove(list.size() - 1);
        }
    }
}
